import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@WebServlet(name = "SmsCodeServlet",urlPatterns = "/smscode.do")
public class SmsCodeServlet extends HttpServlet {

    private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String successJson="{\"msg\":\"success\",\"data\":{\"info\":{\"osn\":\""+randRomOsn()+"\"}},\"status\":true}";
        String failJson="{\"msg\":\"error 信息\",\"status\":false}";
        String cid=request.getParameter("cid");
        String pid=request.getParameter("pid");
        String phone=request.getParameter("phone");
        System.out.println(sdf.format(new Date())+" cid:"+cid+" pid:"+pid+" phone:"+phone);
        if ("".equals(cid)||"".equals(pid)||"".equals(phone)||cid==null||pid==null||phone==null){
            responseContent(response,failJson);
        }else{
            responseContent(response,successJson);
        }
    }

    public  String randRomOsn(){
        String osn="";
        Random rnd=new Random();
        for (int i=0;i<9;i++){
            osn+=rnd.nextInt(10);
        }
        return osn;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        doPost(request,response);
    }


    private void responseContent(HttpServletResponse response,String content) throws IOException {
        PrintWriter writer=response.getWriter();
        writer.write(content);
        writer.close();
    }
}
